<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div>高阶函数</div>
    <script>
      //模仿foreach
      function forEach(array, fn) {
        for (let i = 0; i < array.length; i++) {
          fn(array[i]);
        }
      }
      let arr1 = [1, 3, 5, 7, 9];
      forEach(arr1, (item) => console.log(item));

      //   模拟filter
      function filter(array, fn) {
        let results = [];
        for (let i = 0; i < array.length; i++) {
          if (fn(array[i])) {
            results.push(array[i]);
          }
        }
        return results;
      }

      let arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
      let a = filter(arr2, (item) => {
        return item % 2 === 0;
      });
      console.log(a);
    </script>
  </body>
</html>
